package com.ruoyi.web.service.Impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.MesMakeVersion;
import com.ruoyi.web.domain.MesMakeversionDetail;
import com.ruoyi.web.mapper.MesMakeVersionMapper;
import com.ruoyi.web.service.ProduceService;
import com.ruoyi.web.vo.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.MesPlatemakingDetail;
import com.ruoyi.web.mapper.MesPlatemakingMapper;
import com.ruoyi.web.domain.MesPlatemaking;
import com.ruoyi.web.service.IMesPlatemakingService;

import javax.annotation.Resource;

/**
 * 制版表Service业务层处理
 * 
 * @author wxt
 * @date 2024-02-03
 */
@Service
public class MesPlatemakingServiceImpl implements IMesPlatemakingService 
{
    @Autowired
    private MesPlatemakingMapper mesPlatemakingMapper;

    @Autowired
    private MesMakeVersionMapper mesMakeVersionMapper;

    @Resource
    private ProduceService produceService;
    /**
     * 查询制版表
     * 
     * @param id 制版表主键
     * @return 制版表
     */
    @Override
    public MesPlatemaking selectMesPlatemakingById(Long id)
    {
        MesPlatemaking data= mesPlatemakingMapper.selectMesPlatemakingById(id);
        return data;
    }

    /**
     * 查询制版表列表
     * 
     * @param mesPlatemaking 制版表
     * @return 制版表
     */
    @Override
    public List<MesPlatemaking> selectMesPlatemakingList(MesPlatemaking mesPlatemaking)
    {
        return mesPlatemakingMapper.selectMesPlatemakingList(mesPlatemaking);
    }

    @Override
    public List<MesPlatemakingDetail> getDetailList(MesPlatemakingDetail mesPlatemakingDetail)
    {
        return mesPlatemakingMapper.getDetailList(mesPlatemakingDetail);
    }

    /**
     * 新增制版表
     * 
     * @param mesPlatemaking 制版表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesPlatemaking(MesPlatemaking mesPlatemaking)
    {
        mesPlatemaking.setCreateTime(DateUtils.getNowDate());
        mesPlatemaking.setStatus(2);
        // 生成制版单编号
        int maxNum = mesPlatemakingMapper.selectMaxId();
        String code = PinYinUtils.getCodeNormal(maxNum, "ZBGL");
        mesPlatemaking.setPlatemakingCode(code);

        int rows = mesPlatemakingMapper.insertMesPlatemaking(mesPlatemaking);
        insertMesPlatemakingDetail(mesPlatemaking);
        List<MesPlatemakingDetail> platemakingDetails=mesPlatemaking.getMesPlatemakingDetailList();

//        MesPlatemaking Info= mesPlatemakingMapper.selectMesPlatemakingById(mesPlatemaking.getId());//查询新增的
//        List<MesPlatemakingDetail> mesDetailList = Info.getMesPlatemakingDetailList();
        if (platemakingDetails!=null&&platemakingDetails.size()>0) {
            for(MesPlatemakingDetail detail:platemakingDetails){
                String detailId= detail.getDetailId();
                if(StringUtils.isNotEmpty(detail.getType())&& (detail.getType().equals("新版") ||  detail.getType().equals("旧版"))){
                    //修改做版细表中的状态
                    mesPlatemakingMapper.updateMakeversionDetail(detailId);
                }else if(StringUtils.isNotEmpty(detail.getType())&& detail.getType().equals("补版")){
                    //修改补版细表中的状态
                    mesPlatemakingMapper.updateReplenishDetail(detailId);
                }
            }
//             List<String> ProduceId=  mesDetailList.stream()
//            .map(MesPlatemakingDetail::getProduceId).distinct().collect(Collectors.toList());
            //只查新版的
            List<String> ProduceId=  platemakingDetails.stream()
                    .filter(x -> x.getType().equals("新版")||x.getType().equals("旧版"))
                    .map(MesPlatemakingDetail::getProduceId).distinct().collect(Collectors.toList());
            if (!ProduceId.isEmpty()) {
                for (String produceid : ProduceId) {
                    MesMakeversionDetail makede = new MesMakeversionDetail();
                    Produce produce = produceService.getById(produceid);
                    String makeStatus=produce.getStatus();
                    //20240624 updated 如果状态是已取版，再制版，生产单状态会变成已制版导致不能报工
                    if(StringUtils.isNotEmpty(makeStatus) && Integer.parseInt(makeStatus)>3 && Integer.parseInt(makeStatus)<19 ) {
                    }else{
                        makede.setProduceId(produceid);//生产单id
                        makede.setMakeStatus("4");//生产单状态(已制版)
                        mesMakeVersionMapper.updateproducestatus(makede);
                    }
                }
            }
        }
        return rows;
    }

    /**
     * 修改制版表
     * 
     * @param mesPlatemaking 制版表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesPlatemaking(MesPlatemaking mesPlatemaking)
    {
        if (mesPlatemaking.getStatus()==2){//提交

            MesPlatemaking Info= mesPlatemakingMapper.selectMesPlatemakingById(mesPlatemaking.getId());//查询数据库
            List<MesPlatemakingDetail> mesDetailList = Info.getMesPlatemakingDetailList();
            if (mesDetailList.size()!=0) {
                List<String> ProducePrintId = mesDetailList.stream().map(it -> it.getProducePrintId()).collect(Collectors.toList());
                List<String> ProduceId=  mesDetailList.stream().map(MesPlatemakingDetail::getProduceId).distinct().collect(Collectors.toList());
                String[] ProducePrintIds = ProducePrintId.toArray(new String[0]);//生产印刷单id

                MesMakeversionDetail makede=new MesMakeversionDetail();
//                makede.setProducePrintIds(ProducePrintIds);//生产印刷单集合
//                makede.setMakeStatus("2");//生产印刷单状态
                //查询是否制版
//                int ismake = mesMakeVersionMapper.getismakes(makede);
//                if (ismake>0){
//                    throw new ServiceException("部分印刷单已提交制版，不可重复提交");
//                }
                //批量修改状态为制版
//                mesMakeVersionMapper.updateismakes(makede);
                for (String produceid:ProduceId){
                    makede.setProduceId(produceid);//生产单id
//                    makede.setMakeStatus("2");//生产印刷单状态
//                    int count = mesMakeVersionMapper.getroducesismake(makede);//查询生产单下印刷单没有制版的数量
//                    if (count==0){//修改生产单状态
                        makede.setMakeStatus("4");//生产单状态(已制版)
                        mesMakeVersionMapper.updateproducestatus(makede);
//                    }
                }
            }


        }else{
            mesPlatemakingMapper.deleteMesPlatemakingDetailByPlatemakingId(mesPlatemaking.getId());
            insertMesPlatemakingDetail(mesPlatemaking);
        }
        return mesPlatemakingMapper.updateMesPlatemaking(mesPlatemaking);
    }

    /**
     * 批量删除制版表
     * 
     * @param ids 需要删除的制版表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesPlatemakingByIds(Long[] ids)
    {
        mesPlatemakingMapper.deleteMesPlatemakingDetailByPlatemakingIds(ids);
        return mesPlatemakingMapper.deleteMesPlatemakingByIds(ids);
    }

    /**
     * 删除制版表信息
     * 
     * @param id 制版表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesPlatemakingById(Long id)
    {
        mesPlatemakingMapper.deleteMesPlatemakingDetailByPlatemakingId(id);
        return mesPlatemakingMapper.deleteMesPlatemakingById(id);
    }

    /**
     * 新增制版详情信息
     * 
     * @param mesPlatemaking 制版表对象
     */
    public void insertMesPlatemakingDetail(MesPlatemaking mesPlatemaking)
    {
        List<MesPlatemakingDetail> mesPlatemakingDetailList = mesPlatemaking.getMesPlatemakingDetailList();
        Long id = mesPlatemaking.getId();
        if (StringUtils.isNotNull(mesPlatemakingDetailList))
        {
            List<MesPlatemakingDetail> list = new ArrayList<MesPlatemakingDetail>();
            int xh = 0;
            for (MesPlatemakingDetail mesPlatemakingDetail : mesPlatemakingDetailList)
            {
                mesPlatemakingDetail.setPlatemakingId(id);
                list.add(mesPlatemakingDetail);

                //制版序列号
                xh = xh + 1;
                String xlh = PinYinUtils.hundred(xh);//序列号
                mesPlatemakingDetail.setPlatemakingNo(mesPlatemaking.getPlatemakingCode() + "ZB" + xlh);
            }
            if (list.size() > 0)
            {
                mesPlatemakingMapper.batchMesPlatemakingDetail(list);
            }
        }
    }
}
